package fictional.GUI;

import fictional.Common.CharacterItem;
import fictional.Common.IBL;
import fictional.Common.ImageItem;
import fictional.Common.Response;
import java.io.IOException;

/**
 * This task adds and removes images from the logged in user's
 * image collection.
 */
public class AddRemovePictureTask extends DefaultTask
{
    private String imageURL; 
    ImageItem image;
    boolean isDeleteImage;
    CharacterItem character;
    
    public AddRemovePictureTask(org.jdesktop.application.Application app)
    {
        super(app);
    }
    
    public void setAddTask(IBL BL, GUI gui, String imageURL, CharacterItem character)
    {
        this.BL = BL;
        this.gui = gui;
        this.imageURL = imageURL;
        this.character = character;
    }
    
    public void setDeleteTask(IBL BL, GUI gui, ImageItem image, CharacterItem character)
    {
        this.BL = BL;
        this.gui = gui;
        this.image = image;
        isDeleteImage = true;
        this.character = character;
    }
    
    public void setProfileTask(IBL BL, GUI gui, ImageItem image, CharacterItem character)
    {
        this.BL = BL;
        this.gui = gui;
        this.image = image;
        this.character = character;
    }

    @Override
    protected Object doInBackground() throws IOException, InterruptedException
    {
    	response = new Response();
    	
    	// this is an add task
        if (image == null)
        {
        	BL.insertCharacterImage(character, imageURL, response);
        	if (response.isIs_Success())
        	{
            	gui.setToDraw(true);
        		gui.initPictures();
        	}
        	else
        	{
        		errorTitle = "Error Adding New Image";
        	}
        }
        // this is a delete task
        else if (isDeleteImage)
        {
        	BL.deleteCharacterImage(character, image, response);
        	if (response.isIs_Success())
        	{
            	gui.setToDraw(true);
        		gui.initPictures();
        		if (image.Is_profile())
        		{
        			gui.initProfileImageDisplay();
        		}
        	}
        	else
        	{
        		errorTitle = "Error Deleting Image";
        	}
        }
        else
        {
        	BL.setProfileImage(image, character, response);
        	if (response.isIs_Success())
        	{
            	gui.setToDraw(true);
        		gui.initProfileImageDisplay();
        	}
        	else
        	{
        		errorTitle = "Error Setting Profile Image";
        	}
        }
        
        return null;
    }
}
